
def unwrapv(inph,in_p=(), uv=2*pi):
    """Return the input matrix unwraped the value given in uv
    
    This is a vectorized routine, but is not as fast as it should
    """
    
    if not is_masked(inph):
        fasei=MaskedArray(inph, isnan(inph))
    else:
        fasei=inph.copy()
        
    
    size=fasei.shape
    nx, ny=size
    # If the initial unwraping point is not given, take the center of the image
    # as initial coordinate
    if in_p==():
        in_p=(int(size[0]/2),int(size[1]/2))

    # Create a temporal space to mark if the points are already unwrapped
    # 0 the point has not been unwrapped
    # 1 the point has not been unwrapped, but it is in the unwrapping list 
    # 2 the point was already unwrapped

    fl=N.zeros(size)

    # List containing the points to unwrap
    l_un=[in_p]
    fl[in_p]=1

    # unwrapped values
    faseo=fasei.copy()
    XI_, YI_= meshgrid(range(-1, 2), range(-1, 2))
    XI_=XI_.flatten()
    YI_=YI_.flatten()
    while len(l_un)>0:
        # remove the first value from the list
        unp=l_un.pop(0)
        #l_un[0:1]=[]
        XI=XI_+unp[0]
        YI=YI_+unp[1]
        #Remove from the list the values where XI is negative
        nxi=XI>-1
        nyi=YI>-1
        nxf=XI<nx
        nyf=YI<ny
        n=nonzero(nxi& nyi & nxf & nyf)
        lco=zip(XI[n], YI[n])
        
        # Put the coordinates of unwrapped the neigbors in the list
        
        
        # And check for wrapping
        nv=0
        wv=0    
        
        
        for co in lco:
            if (fl[co]==0) & (faseo.mask[co]==False):
                fl[co]=1
                l_un.append(co)
            elif fl[co]==2:
                wv=wv+rint((faseo[co]-faseo[unp])/uv)
                nv=nv+1
    
        if nv!=0: 
            wv=wv/nv
            #if wv>=0: wv=int(wv+0.5)
            #else: wv=int(wv-0.5)
        fl[unp]=2
        faseo[unp]=faseo[unp]+wv*uv
    
    return faseo


